// pages/myself/info/info.js
var network = require('../../../utils/network_util.js');
var api = require('../../../utils/api.js');
var utils = require('../../../utils/utils.js');
var Zan = require('../../../dist/index');
const app = getApp();
Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    // sex: ['男', '女'], // 男=1， 女=2
    // sexIndex: 0,
    date: '2017-10-01',
    avatarUrl: null,
    nickName: null,
    gender: '未知',
    canIUseMobile: wx.canIUse('button.open-type.getPhoneNumber')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "个人资料",
    })
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        // 可以将 res 发送给后台解码出 unionId
        app.globalData.userInfo = res.userInfo
        that.setData({
          avatarUrl: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName,
        })
        if (res.userInfo.gender == 0) {
          that.setData({
            gender: '未知'
          })
        } else if (res.userInfo.gender == 1) {
          that.setData({
            gender: '男'
          })
        } else if (res.userInfo.gender == 2) {
          that.setData({
            gender: '女'
          })
        }
      }
    })

    /**
      * 获取个人及商家信息
      */
    if (app.globalData.userAndTenantInfo) {
      this.setData({
        // avatarUrl: app.globalData.userInfo.avatarUrl,
        // nickName: app.globalData.userInfo.nickName,
        name: app.globalData.userAndTenantInfo.name,
        // gender: app.globalData.userInfo.gender == 1 ? '男' : '女',
        date: utils.formatDate02(new Date(app.globalData.userAndTenantInfo.dateOfBirth)),
      })
      if (app.globalData.userAndTenantInfo.mobile) {
        this.setData({
          mobile: app.globalData.userAndTenantInfo.mobile.substr(0, 3) + "****" + app.globalData.userAndTenantInfo.mobile.substr(7)
        })
      }
    } else {
      app.userAndTenantInfoCallback = res => {
        this.setData({
          // avatarUrl: res.avatarUrl,
          // nickName: res.nickName,
          name: res.name,
          // gender: res.gender == 1 ? '男' : '女',
          date: utils.formatDate02(new Date(res.dateOfBirth)),
        })
        if (res.mobile) {
          this.setData({
            mobile: res.mobile.substr(0, 3) + "****" + res.mobile.substr(7)
          })
        }
      }
    }

    if (!this.data.mobile) {
      this.setData({
        mobile: ""
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 选择性别
   */
  // bindPickerChange: function (e) {
  //   this.setData({
  //     sexIndex: e.detail.value
  //   })
  // },
  /**
   * 日期
   */
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  /**
   * 修改资料
   */
  formSubmit: function (e) {
    var name = e.detail.value.name;
    //var sex = parseInt(this.data.sexIndex) + 1;
    this.setData({
      name: name,
    })

    if (app.globalData.userInfo) {
      network._post_json(this, api.modifyUserInfo, { nickName: app.globalData.userInfo.nickName, avatarUrl: app.globalData.userInfo.avatarUrl, name: name, gender: app.globalData.userInfo.gender, dateOfBirth: this.data.date }, success);
    } else {
      app.userInfoReadyCallback = res => {
        network._post_json(this, api.modifyUserInfo, { nickName: res.userInfo.nickName, avatarUrl: res.userInfo.avatarUrl, name: name, gender: res.gender, dateOfBirth: this.data.date }, success);
      }
    }
  },

  getPhoneNumber: function (e) {
    if (e.detail.encryptedData) {
      network._post_json(this, api.wxphone, { encryptedData: e.detail.encryptedData, iv: e.detail.iv, sessionKey: app.globalData.sessionKey }, bindSuccess);
    }
  },

  // refresh: function (mobile) {
  //   this.setData({
  //     mobile: mobile
  //   })
  // }

}))

var success = function (that, res) {
  that.showZanToast('资料修改成功');
  app.globalData.userAndTenantInfo.name = that.data.name;
  app.globalData.userAndTenantInfo.gender = app.globalData.userInfo.gender;
  app.globalData.userAndTenantInfo.dateOfBirth = new Date(that.data.date).getTime();
}

var bindSuccess = function (that, res) {
  app.globalData.userAndTenantInfo.mobile = res.data.data.mobile;
  that.setData({
    mobile: res.data.data.mobile.substr(0, 3) + "****" + res.data.data.mobile.substr(7)
  })
}